/**
 * 
 */
package by.wind.drebedengi;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import by.wind.drebedengi.database.MyDatabaseHelper;
import by.wind.drebedengi.datalayer.Constants;
import by.wind.drebedengi.datalayer.GetAccessStatus;
import by.wind.drebedengi.datalayer.bean.Login;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

/**
 * @author wind
 *
 */
public class LoginActivity extends OrmLiteBaseActivity<MyDatabaseHelper> {

	private Login itsLogin;
	
	private GetAccessStatus itsAccessStatus;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		
		Button sendButton = (Button) findViewById(R.id.login_sendbutton);
		sendButton.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				final EditText username = (EditText) findViewById(R.id.login_username);
				final EditText password = (EditText) findViewById(R.id.login_password);
				if (username.getText().toString().equals("")) {
					Toast.makeText(getApplicationContext(), getText(R.string.login_empty_username), Toast.LENGTH_LONG).show();
					return;
				}
				if (password.getText().toString().equals("")) {
					Toast.makeText(getApplicationContext(), getText(R.string.login_empty_password), Toast.LENGTH_LONG).show();
					return;
				}
				itsLogin = new Login();
				itsLogin.setLogin(username.getText().toString());
				itsLogin.setPassword(password.getText().toString());
				itsAccessStatus = new GetAccessStatus(username.getText().toString(),password.getText().toString());
				
				new AsyncTask<Integer, Integer, Boolean>()
		        {
					ProgressDialog progressDialog;
					
					@Override
					protected void onPreExecute() {
						 /* This is executed on UI thread before doInBackground(). It is
		                 * the perfect place to show the progress dialog.
		                 */
		                progressDialog = ProgressDialog.show(LoginActivity.this, "",
		                        getString(R.string.task_loading));
					}
					
					@Override
					protected Boolean doInBackground(Integer... params) {
						// TODO Auto-generated method stub
						if (params == null)
		                {
		                    return false;
		                }
						try
		                {
		                    /*
		                     * This is run on a background thread, so we can sleep here
		                     * or do whatever we want without blocking UI thread. A more
		                     * advanced use would download chunks of fixed size and call
		                     * publishProgress();
		                     */
							itsAccessStatus.loadData();
							if (itsAccessStatus.getResult()) {
								getHelper().save(itsLogin);
			                    // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME
								Constants.LOGIN_VALUE = username.getText().toString();
								Constants.PASS_VALUE = password.getText().toString();
								
							}
								
		                }
		                catch (Exception e)
		                {
		                    Log.e("tag", e.getMessage());
		                    /*
		                     * The task failed
		                     */
		                    return false;
		                }

		                /*
		                 * The task succeeded
		                 */
		                return true;
					}
					
					@Override
		            protected void onPostExecute(Boolean result)
		            {
		                progressDialog.dismiss();
		                /*
		                 * Update here your view objects with content from download. It
		                 * is save to dismiss dialogs, update views, etc., since we are
		                 * working on UI thread.
		                 */
		                if (!result)
		                {
		                	AlertDialog.Builder b = new AlertDialog.Builder(LoginActivity.this);
			                b.setTitle(android.R.string.dialog_alert_title);
			                b.setMessage(getText(R.string.login_failed));
			                b.setPositiveButton(getString(android.R.string.ok),
			                        new DialogInterface.OnClickListener()
			                        {
			                            @Override
			                            public void onClick(DialogInterface dlg, int arg1)
			                            {
			                                dlg.dismiss();
			                            }
			                        });
			                b.create().show();
		                } else {
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
    						startActivity(intent);
		                }
		             
		                
		                
		            }
					
					
		        }.execute();
			}
		});
	}
}
